<template>
  <div class="course-content">
    <div class="content-header">
      <el-page-header
        @back="goBack"
        :content="course.courseName"
      ></el-page-header>
      <el-button type="primary" @click="handleSectionDlg()"
        >+添加章节</el-button
      >
    </div>

    <el-card class="box-card">
      <el-tree :data="courseContent" :props="defaultProps">
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span>{{ node.label }}</span>
          <span v-if="data.sectionName">
            <el-button @click="handleSectionDlg(data)"> 编辑 </el-button>
            <el-button type="primary"> 添加课时 </el-button>
            <el-button> 已隐藏 </el-button>
          </span>
          <span v-if="data.theme">
            <el-button> 编辑 </el-button>
            <el-button type="success"> 上传视频 </el-button>
            <el-button> 已隐藏 </el-button>
          </span>
        </span>
      </el-tree>
    </el-card>
    <section-dialog-create-or-edit
      ref="sectionDlg"
      @change="loadCourseContent"
    ></section-dialog-create-or-edit>
  </div>
</template>
<script>
import { getCourseContent } from '@/api/course-content'
import { getCourseInfo } from '@/api/course'
import SectionDialogCreateOrEdit from '@/views/course-content/section-dialog-create-or-edit.vue'

export default {
  name: 'CourseContent',
  components: { SectionDialogCreateOrEdit },
  props: {
    courseId: {
      type: [String, Number]
    }
  },
  data () {
    return {
      courseContent: [],
      course: {},
      defaultProps: {
        children: 'lessonDTOS',
        label: function (data, node) {
          return data.sectionName || data.theme
        }
      }
    }
  },
  created () {
    console.log(this.courseId)
    this.loadCourseContent()
    this.loadCourse()
  },
  methods: {
    loadCourse () {
      getCourseInfo(this.courseId).then((res) => {
        if (res.data.code === '000000') {
          this.course = res.data.data
        } else {
          this.$message.error('获取课程详情失败')
        }
      })
    },
    loadCourseContent () {
      getCourseContent(this.courseId).then((res) => {
        if (res.data.code === '000000') {
          this.courseContent = res.data.data
        } else {
          this.$message.error('获取课程内容失败')
        }
      })
    },
    goBack () {
      this.$router.go(-1)
    },
    handleSectionDlg (section) {
      this.$refs.sectionDlg.showAndInit(this.course, section)
    }
  }
}
</script>
<style scoped lang="scss">
.content-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
}

.el-page-header {
  align-items: center;
}

::v-deep .el-tree-node__content {
  padding: 15px 0px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
